﻿using B.S.BaseData.Api.Write.Applicantion.Command;
using B.S.BaseData.ErrorCode;
using B.S.BaseData.Infrastructure.Interface;
using B.S.BataBase.Domain;
using MediatR;

namespace B.S.BaseData.Api.Write.Applicantion.Handler
{
    public class UserStateUpdateCommandHandler : IRequestHandler<UserStateUpdateCommand, APIResult<int>>
    {
        private readonly IBaseRepository<UserModel> _repository;

        public UserStateUpdateCommandHandler(IBaseRepository<UserModel> repository)
        {
            _repository = repository;
        }

        public async Task<APIResult<int>> Handle(UserStateUpdateCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result = new APIResult<int>();

            var list = await _repository.GetValues(x => x.Id == request.Id);
            if (list.UserState == true)
            {
                list.UserState = false;
            }
            else
            {
                list.UserState = true;
            }
            await _repository.UpdateAsync(list);
            result.Code = ApiEnum.Success;
            result.Data = 1;
            result.Msg = "用户状态修改成功";
            return result;
        }
    }
}
